package servlet.permission;

import common.Fid_menu;
import common.Module_role;
import common.Page;
import pojo.Function;
import pojo.SecoundMenu;
import service.PermissionService;
import service.serviceImpl.PermissionServiceImpl;
import servlet.BaseServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017-12-10.
 */
public class PermissionServlet extends BaseServlet {
    int result=0;
    private static int PAGE_SIZE=10;
    PermissionService permissionService=new PermissionServiceImpl();
    public String tiaozhuan(HttpServletRequest request, HttpServletResponse response){
        return "/jsp/permission/addSecoundMenu.jsp";
    }
    //增加功能模块
    public String addSecoundMenu(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        SecoundMenu secoundMenu=new SecoundMenu();
        secoundMenu.setFid(Fid_menu.getFid((request.getParameter("firstMenu"))));
        secoundMenu.setUrl(request.getParameter("url"));
        secoundMenu.setSecoundMenu(request.getParameter("secoundMenu"));
        result= permissionService.addSecoundMenu(secoundMenu);
        if (result>0){
        }
        return "/home?method=home";
    }
    //模块管理
    public String manageModule(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //得到全部的功能菜单
        List<Function> functionList= permissionService.manageModule();
        Page page=new Page();
        int pageNow=page.currentPageNow(request,functionList.size());
        List<Function> functions=permissionService.getCurrFunction(pageNow,PAGE_SIZE);
        request.setAttribute("functions",functions);
        return "/jsp/permission/manageModule.jsp";
    }
    //打开功能(就是能实现功能)
    public String openFunction(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        int id=Integer.parseInt(request.getParameter("id"));

        String functionDoc="打开";
        result=permissionService.openFunction(id,functionDoc);
        return "/home?method=home";
    }
    //关闭功能
    public String closeFunction(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        int id=Integer.parseInt(request.getParameter("id"));

        String functionDoc="关闭";
        result=permissionService.openFunction(id,functionDoc);
        return "/home?method=home";
    }
    //删除权限
    public String delFunction(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        int id=Integer.parseInt(request.getParameter("id"));

        result=permissionService.delFunction(id);
        return "/home?method=home";
    }
    public String give(HttpServletRequest request,HttpServletResponse response) throws SQLException {

        List<SecoundMenu> secoundMenuList= permissionService.getSecoundMenu();
        request.setAttribute("secoundMenuList",secoundMenuList);
        return "jsp/permission/giveFunction.jsp";
    }
    //赋予权限
    public String controlFunction(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
        //查看该模块功能是否已经被授予权限
        String moduleName=request.getParameter("moduleName");
        String secoundMenu=request.getParameter("secoundMenu");

        Function function=new Function();
        Function function1= permissionService.checkFunction(moduleName,secoundMenu);
        //判断
        if (function1!=null){
            String meg="该权限以被授予，不需要重新授予";
            request.setAttribute("meg",meg);
            return "/admin?method=manageModule";
        }
        SecoundMenu secoundMenu2= permissionService.getFid(secoundMenu);
        function.setFunctionAddress(secoundMenu2.getUrl());
        function.setModuleName(moduleName);
        function.setFunctionName(secoundMenu);
        function.setFunction(Fid_menu.getFirstMenu(secoundMenu2.getFid()));
        function.setFunctionId(secoundMenu2.getFid());
        function.setModuleId(Module_role.getRole(moduleName));
        result=permissionService.addFunction(function);

        return "/home?method=home";
    }
}
